export function log(str, ...args) {
  // eslint-disable-next-line
  console.log(`%cPeki \٩\(\๑\′0\`\๑\)\۶:${str}`, ...args);
}
export function consoleSuc(str) {
  log(str, 'color:#89BEB2');
}
export function consoleErr(str) {
  log(str, 'color:#FE4365');
}
export function consoleWarn(str) {
  log(str, 'color:#F9CDAD');
}
export function sleep(time) {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => {
      clearTimeout(timer);
      resolve();
    }, time);
  });
}

export function urlGet(getParam) {
  // 取得Get参数
  const _aQuery = decodeURI(window.location.href).split('?');
  const aQuery = getParam || _aQuery[1];
  const aGET = [];
  if (aQuery && aQuery.length > 1) {
    const aBuf = aQuery.split('&');
    for (let i = 0, iLoop = aBuf.length; i < iLoop; i++) {
      // 分离key与Value
      const aTmp = aBuf[i].split('=');
      // eslint-disable-next-line prefer-destructuring
      aGET[aTmp[0]] = aTmp[1];
    }
  }
  return aGET;
}

export function urlAddParam(search, params = {}) {
  let result = search;
  // eslint-disable-next-line
  for (const key in params) {
    const val = params[key];
    const newParam = `${key}=${val}`;
    let aParams = `?${newParam}`;
    if (search) {
      console.log(aParams);
      aParams = search.replace(new RegExp(`[?&]${key}[^&]*`), '');
      console.log(aParams);
      if (aParams === search) {
        result += `&${newParam}`;
      }
    }
  }

  return result;
}

export function concatStr(arr, separator = ',') {
  if (!arr || !arr.length) {
    return '';
  }
  let str = '';
  arr.map((string) => {
    str += `${separator}${string}`;
  });
  return str.substr(1, str.length);
}

export function isNumber(obj) {
  // eslint-disable-next-line no-restricted-globals
  return typeof obj === 'number' && !isNaN(obj);
}

// base64转换成file
export function base64ToFile(base, name, type) {
  const arr = base.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], {type: mime});
}

// 秒转时分秒
export function formatSeconds(second) {
  if (second < 0) return [0, 0, 0];
  const sec = Math.floor(second);
  const h = Math.floor(sec / 3600);
  const m = Math.floor((sec % 3600) / 60);
  const s = Math.floor(sec % 60);
  return [h, m, s];
}

// 获取元素的纵坐标（相对于窗口）
export function getTop(e) {
  let offset = e.offsetTop;
  if (e.offsetParent != null) offset += getTop(e.offsetParent);
  return offset;
}

// 获取元素的横坐标（相对于窗口）
export function getLeft(e) {
  let offset = e.offsetLeft;
  if (e.offsetParent != null) offset += getLeft(e.offsetParent);
  return offset;
}

// 是否开发环境
export function isNodeEnvDev() {
  return process.env.NODE_ENV === 'developement';
}

// 是否自定义测试环境
export function isTesting() {
  const querys = urlGet();
  const testing = +querys.testing;
  return testing || isNodeEnvDev();
}

// v-html 换行处理
export function filterVhtmlLine(str) {
  if (str.indexOf('\n') > -1) {
    str = str.replace(/\n/g, '<br/>');
  }
  return str;
}

/**
 * 去除字符串空格
 * @param str 传入的字符串
 */
export function wipeOffSpacing(str) {
  if (str && /\s/g.test(str)) {
    const newStr = str.replace(/\s/g, '');
    str = newStr;
  }
  return str;
}
/**
 * 首字母大写
 * @param str 传入的字符串
 */
export function firstLetterUpper(str) {
  if (str) {
    str = str.charAt(0).toUpperCase() + str.slice(1);
  }
  return str;
}

/**
 * 插入KeyFrame
 * @param rule 值
 */
export function insertKeyFrame(rule) {
  const style = document.createElement('style');
  style.innerHTML = rule;
  document.head.appendChild(style);
}

function addZero(i) {
  if (i < 0) return i;
  if (i < 10) {
    i = `0${i}`;
  }
  return i;
}

export const timeCountDown = (startTime, curTime = Date.now()) => {
  startTime = new Date(startTime).getTime();
  const leftTime = parseInt((curTime - startTime) / 1000, 10);
  const d = parseInt(leftTime / (24 * 60 * 60), 10);
  let h = parseInt((leftTime / (60 * 60)) % 24, 10);
  let m = parseInt((leftTime / 60) % 60, 10);
  let s = parseInt(leftTime % 60, 10);
  h = addZero(h);
  m = addZero(m);
  s = addZero(s);

  return {
    d,
    h,
    m,
    s
  };
};

export const deepCopy = (object) => JSON.parse(JSON.stringify(object));
